/*******************************************************************************
 * Copyright (c) 2011 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
package nz.co.senanque.perspectivemanager;

import java.io.IOException;

import org.springframework.core.io.Resource;

import com.vaadin.Application;
import com.vaadin.service.FileTypeResolver;
import com.vaadin.terminal.ApplicationResource;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.Terminal.ErrorEvent;

/**
 * This delivers a Spring Resource as a Vaadin Resource.
 * Not actually used but might be useful sometime.
 * 
 * @author Roger Parkinson
 *
 */
public class SpringResource implements ApplicationResource {

    private static final long serialVersionUID = 3634111017950912903L;
	/**
     * Explicit mime-type.
     */
    private String MIMEType = null;
    /**
     * Default buffer size for this stream resource.
     */
    private int bufferSize = 0;

    /**
     * Resource where the downloaded content is fetched from.
     */
    private Resource sourceResource;

    /**
     * Application.
     */
    private final Application application;

    /**
     * Default cache time for this stream resource.
     */
    private long cacheTime = DownloadStream.DEFAULT_CACHETIME;

    /**
     * Creates a new file resource for providing given file for client
     * terminals.
     */
    public SpringResource(Resource resource, Application application) {
        this.application = application;
        sourceResource = resource;
        application.addResource(this);
    }
    /**
     * @see com.vaadin.terminal.Resource#getMIMEType()
     */
    public String getMIMEType() {
        if (MIMEType != null) {
            return MIMEType;
        }
        return FileTypeResolver.getMIMEType(sourceResource.getFilename());
    }

    /**
     * Sets the mime type of the resource.
     * 
     * @param MIMEType
     *            the MIME type to be set.
     */
    public void setMIMEType(String MIMEType) {
        this.MIMEType = MIMEType;
    }

	public DownloadStream getStream() {
        try {
            final DownloadStream ds = new DownloadStream(sourceResource.getInputStream()
            		, getMIMEType(), getFilename());
            ds.setBufferSize(getBufferSize());
            ds.setCacheTime(cacheTime);
            return ds;
        } catch (final IOException e) {
            // Log the exception using the application error handler
            getApplication().getErrorHandler().terminalError(new ErrorEvent() {

                private static final long serialVersionUID = -5482850045663077875L;

				public Throwable getThrowable() {
                    return e;
                }

            });
        }
            return null;
	}

	public Application getApplication() {
		return application;
	}

	public String getFilename() {
		return sourceResource.getFilename();
	}

	public long getCacheTime() {
		return cacheTime;
	}

	public int getBufferSize() {
		return bufferSize;
	}

}
